<?php

namespace app\admin\model;

use think\Model;

use think\Db;
use think\Request;
use think\Exception;//事务处理

class Journal extends Model
{
    //日记账登记
    public function getjournal($res)
    {
        $lang = $res['lang'];
        $moneyfive = $moneythree = $moneytwo = $moneyone = 0.000;
        $admin = session('admin');
        $dataaccounts = '';
        if ($admin['position_id'] != 1) {
            //处理记账科目的显示
            $posid = $admin['position_id'];
            $whereacc[] = ["exp", "FIND_IN_SET($posid,position_ids)"];
            //授权的科目
            $accountdata = Db::name('fin_account')->field('id,name,pid')->where($whereacc)->select();
            $ids = array_column($accountdata, 'id');
            //获取授权科目的父级
            $accid = array();
            foreach ($accountdata as $acckey => $accountval) {
                $accountdata[$acckey]['disable'] = 2;//能够选择
                $idsacc = getpostion('fin_account', $accountval['id']);
                $chaacc = array_diff($idsacc, $ids);
                foreach ($chaacc as $chaacckey => $chaaccval) {
                    $accinfo = Db::name('fin_account')->field('id,pid,name')->where('id', $chaaccval)->find();
                    if (!in_array($accinfo['id'], $accid)) {
                        array_push($accid, $accinfo['id']);
                        $accinfo['disable'] = 1;//不能选择
                        $accoundatas[] = $accinfo;
                    }
                }
            }
            //记账科目的显示
            $dataaccounts = getTree(array_merge($accountdata, $accoundatas));
        }

        if ($lang != 'zh') {
            $dateday = getTimeDate(null, 13);
        } else {
            $dateday = getTimeDate(null, 7);
        }


        $mapwhere['b.country_id'] = $res['country_id'];//国家选择
        if ($admin['position_id'] != 1) {
            $country_poid = getinfo($admin['position_id'], 'position')['country_id'];
            //查看权限
            $admin_authids = explode(",", $admin['auth_ids']);//权限
            $posids = array_column(getFrames('position', null, $country_poid), 'id');
            array_push($posids, $admin['position_id']);//获取的是所有的职位
            $adminids = array_column(Db::name('admin')->field('id')->where('position_id', 'in', $posids)->select(), 'id');//获取能查看的员工的ids


            if (in_array('177', $admin_authids) && !in_array('178', $admin_authids)) {
                //查看自己下级的员工的日记账
                $mapwhere['a.create_id'] = ['in', $adminids];
            } elseif (in_array('176', $admin_authids) && !in_array('177', $admin_authids) && !in_array('178', $admin_authids)) {
                //只能看自己的账单
                $mapwhere['a.create_id'] = $admin['id'];
            } else {
                $mapwhere['a.create_id'] = $admin['id'];
            }
//            $posid = $admin['position_id'];
//            $mapwhere[] = ["exp", "FIND_IN_SET($posid,position_ids)"];
        }
        if (isset($res['journal']) && $res['journal'] == 'day') {
            $data = $this->alias('a')->join('nt_fin_account b', 'a.account_id=b.id', 'left')->field('a.*,b.name,b.adjust,b.pid')->where($mapwhere)->page($res['page'], $res['pageSize'])->order('a.happen_day desc')->select();

            $count = $this->alias('a')->join('nt_fin_account b', 'a.account_id=b.id', 'left')->where($mapwhere)->count();

        } elseif (isset($res['journal']) && $res['journal'] == 'month') {
            //月记账
            $years = $res['year'];
            $months = $res['month'];

            $yearsmonths = $years . '-' . $months;
            $yearmonth = getNextMonthDays($yearsmonths);
            $startime = strtotime($yearmonth[0]);//当月开始的时间戳
            $lasttime = strtotime($yearmonth[1]);//当月结束的时间戳
            $mapwhere['a.happen_day'] = ['between', "$startime,$lasttime"];
            //计算上月结余
            if ($months - 1 == 0) {
                //获取的去年的12月的结余
                $maplast['year'] = $years - 1;
                $maplast['month'] = 12;
            } else {
                $maplast['year'] = $years;
                $maplast['month'] = $months - 1;
            }
            if ($admin['position_id'] != 1) {
                //获取个人的上月结余
                $maplast['admin_id'] = session('userid');
                $moneyfive = Db::name('journal_month')->where($maplast)->value('amount');
            } else {
                //计算管理员的上月结余
                $moneyfive = Db::name('journal_month')->where($maplast)->sum('amount');
            }
            $datares = Db::name('journal')->alias('a')->join('nt_fin_account b', 'a.account_id=b.id', 'left')->field('a.id,a.account_id,a.happen_day,sum(a.amount) as amount,b.name,b.adjust,b.pid')->where($mapwhere)->order('a.happen_day desc')->group('a.account_id')->select();
            $count = $this->alias('a')->join('nt_fin_account b', 'a.account_id=b.id', 'left')->where($mapwhere)->group('a.account_id')->count();

            //获取年账单的上级科目
            $accounpiddatas = $this->getpiddata($datares);
            //月账单数据
            $data = getTrees(array_merge($accounpiddatas, $datares));
            $count = $this->alias('a')->join('nt_fin_account b', 'a.account_id=b.id', 'left')->where($mapwhere)->group('a.account_id')->count();
        } elseif (isset($res['journal']) && $res['journal'] == 'year') {
            $startyear = $res['year'] . '-01-01';
            $lastyear = $res['year'] . '-12-31';
            $startime = strtotime($startyear);//当年开始的时间戳
            $lasttime = strtotime($lastyear);//当年结束的时间戳
            //年记账
            $mapwhere['a.happen_day'] = ['between', "$startime,$lasttime"];
            $lastyeat['year'] = $res['year'] - 1;
            //计算上一年的结余
            if ($admin['position_id'] != 1) {
                $lastyeat['admin_id'] = session('userid');
                //获取个人的结余年
                $moneyfive = Db::name('journal_year')->where($lastyeat)->value('amount');
            } else {
                //管理员的年结余
                $moneyfive = Db::name('journal_year')->where($lastyeat)->sum('amount');
            }
            $datares = Db::name('journal')->alias('a')->join('nt_fin_account b', 'a.account_id=b.id', 'left')->field('a.id,a.account_id,a.happen_day,sum(a.amount) as amount,b.name,b.adjust,b.pid')->where($mapwhere)->group('a.account_id')->order('a.happen_day desc')->select();
            //获取年账单的上级科目
            $accounpiddatas = $this->getpiddata($datares);
            //月账单数据
            $data = getTrees(array_merge($accounpiddatas, $datares));
            $count = $this->alias('a')->join('nt_fin_account b', 'a.account_id=b.id', 'left')->where($mapwhere)->group('a.account_id')->count();
        }
        $arr = array();
        //对数据进行总的处理
        foreach ($data as $datakey => $dataval) {
            //含有活动时间
            if (isset($dataval['happen_day']) && isset($dataval['update_at'])) {
                $data[$datakey]['update_date'] = getTimeDate($dataval['update_at'], 12);//更新时间
                //日记账的发生时间和更新时间更新者
                if ($lang != 'zh') {
                    //发生日期
                    $datehappen = $data[$datakey]['happen_date'] = getTimeDate($dataval['happen_day'], 8);
                    if (in_array($datehappen, $arr)) {
                        $data[$datakey]['happen_date'] = '';
                    }
                    array_push($arr, $datehappen);
                } else {
                    $data[$datakey]['happen_date'] = $datehappen = getTimeDate($dataval['happen_day'], 7);
                    if (in_array($datehappen, $arr)) {
                        $data[$datakey]['happen_date'] = '';
                    }
                    array_push($arr, $datehappen);
                }
                //编辑者
                $data[$datakey]['updatename'] = getinfo($dataval['update_id'], 'admin')['nickname'];
            }
            //科目名称
            $data[$datakey]['fin_accountname'] = $dataval['name'];

            //获取金额的类别
            $data[$datakey]['adjust'] = $dataval['adjust'];

            //判断是否含有子级
            $infoacc_fu = Db::name('fin_account')->where('pid', $dataval['account_id'])->find();
            if ($infoacc_fu) {
                $data[$datakey]['isexist'] = 1;
            } else {
                $data[$datakey]['isexist'] = 2;
            }

            //处理颜色判断
            if ($dataval['adjust'] == 1) {
                //收入
                $data[$datakey]['color'] = 'blue';
                $moneyone += $dataval['amount'];
            } elseif ($dataval['adjust'] == 2) {
                //支出
                $data[$datakey]['color'] = 'red';
                $moneytwo += $dataval['amount'];
            } elseif ($dataval['adjust'] == 0) {
                //不调整
                $data[$datakey]['color'] = 'gray';
                //备注金额
                $moneythree += $dataval['amount'];
            }
        }
        //页面结余
        $moneyfor = $moneyone - $moneytwo;
        $moneys = array('onemoney' => number_format($moneyone, 3), 'twomoney' => number_format($moneytwo, 3), 'threemoney' => number_format($moneythree, 3), 'formoney' => number_format($moneyfor, 3), 'fivemoney' => number_format($moneyfive, 3));
        return array('count' => $count, 'data' => $data, 'dateday' => $dateday, 'moneys' => $moneys, 'finaccounts' => $dataaccounts);
    }


    public function journaladdedit($data)
    {
        $ckdata = notEmpty(['happen_day' => lang("date"), 'account_id' => lang("account_subject"), 'remarks' => lang("remark"), 'amount' => lang("amount")]);
        if ($ckdata['status']==2) {
            return $ckdata;
        }
        if ($data['amount'] <= 0) {
            return array('msg' => lang("the_amount_can't_be_negative"), 'status' => 0);
        }
        $act = $data['act'];
        unset($data['act']);
        $name = $data['remarks'] = trim($data['remarks']);
        $time = $data['happen_day'] = strtotime($data['happen_day']);
        $data['year'] = getTimeDate($time, 1);
        $data['month'] = getTimeDate($time, 2);
        if ($act == 'journaladd') {
            Db::startTrans();
            $data['create_at'] = $data['update_at'] = time();
            $admin_id = $data['create_id'] = $data['update_id'] = session('userid');
            $jounid = $this->strict(false)->insertGetId($data);//写入日记账表
            //更新这个用户的月账单和年账单
            $updateid = $this->updaemonthyear($admin_id);
            if ($jounid && $updateid) {
                Db::commit();
                return array('msg' => $name . lang("added_successfully"), 'status' => 1);
            } else {
                return array('msg' => $name . lang("add_failed"), 'status' => 0);
            }

        } elseif ($act == 'journaledit') {
            Db::startTrans();
            //更新日账单数据
            $data['update_at'] = time();
            $data['update_id'] = session('userid');
            $jounalid = $this->where('id', $data['id'])->update($data);
            $admin_id = $this->where('id', $data['id'])->value('create_id');
            $updateid = $this->updaemonthyear($admin_id);
            if ($jounalid !== false && $updateid) {
                Db::commit();
                return array('msg' => $name . lang("edited_successfully"), 'status' => 1);
            } else {
                return array('msg' => $name . lang("edit_failed"), 'status' => 0);
            }
        }
    }

    //日记删除
    public function journaldel($data)
    {
        $name = $data['name'];
        $admin_id = $this->where('id', $data['id'])->value('create_id');
        Db::startTrans();

        $retdelid = $this->where('id', $data['id'])->delete();//删除日记账表
        //判断该用户是否还存在数据
        $updateid = $this->updaemonthyear($admin_id);//更新月账单和年账单表
        if ($retdelid && $updateid) {
            Db::commit();
            return array('msg' => $name . lang("deleted_successfully"), 'status' => 1);
        } else {
            return array('msg' => $name . lang("failed_to_delete"), 'status' => 0);
        }
    }

    //月记账和年记账的重新写入
    protected function updaemonthyear($admin_id)
    {
        //删除这个用户之前的月账单和年账单
        Db::name('journal_month')->where('admin_id', $admin_id)->delete();
        Db::name('journal_year')->where('admin_id', $admin_id)->delete();
        //统计每一年每一月
        $monthdata = Db::name('journal')->field('year,month')->where('create_id', $admin_id)->group('year,month')->select();
        foreach ($monthdata as $monthdatakey => $monthdataval) {
            $monthdata[$monthdatakey]['amount'] = $this->getmoney($monthdataval['year'], $monthdataval['month'], $admin_id);
            $monthdata[$monthdatakey]['admin_id'] = $admin_id;
        }
        $yeardata = Db::name('journal')->field('year')->where('create_id', $admin_id)->group('year')->select();
        foreach ($yeardata as $yeardatakey => $yeardataval) {
            $yeardata[$yeardatakey]['amount'] = $this->getmoney($monthdataval['year'], null, $admin_id);
            $yeardata[$yeardatakey]['admin_id'] = $admin_id;
        }
        //将数据写入年账单表和月账单表
        $monthid = Db::name('journal_month')->insertAll($monthdata);
        $yearid = Db::name('journal_year')->insertAll($yeardata);
        return 1;
    }

    protected function getmoney($year, $month, $admin_id)
    {
        $moneyjia = 0;
        $moneyjian = 0;
        $datares = Db::name('journal')->alias('a')->join('nt_fin_account b', 'a.account_id=b.id', 'left')->field('a.*,b.adjust')->where('a.create_id', $admin_id)->select();
        if ($month) {
            //统计月账单的金额
            foreach ($datares as $datareskey => $dataresval) {
                if ($dataresval['year'] == $year && $dataresval['month'] == $month) {
                    if ($dataresval['adjust'] == 1) {
                        //收入
                        $moneyjia += $dataresval['amount'];
                    } elseif ($dataresval['adjust'] == 2) {
                        //支出
                        $moneyjian += $dataresval['amount'];
                    }
                }
            }
        } else {
            //统计年账单的金额
            foreach ($datares as $datareskey => $dataresval) {
                if ($dataresval['year'] == $year) {
                    if ($dataresval['adjust'] == 1) {
                        //收入
                        $moneyjia += $dataresval['amount'];
                    } elseif ($dataresval['adjust'] == 2) {
                        //支出
                        $moneyjian += $dataresval['amount'];
                    }
                }
            }
        }
        //当月结余和年结余
        $moneys = $moneyjia - $moneyjian;
        return $moneys;
    }


    //获取账单的上级科目
    protected function getpiddata($datares)
    {
        $accoundatas = array();
        $ids = array_column($datares, 'account_id');
        $accid = array();
        foreach ($datares as $datareskey => $dataresval) {
            $pidsarr = getpostion('fin_account', $dataresval['account_id']);//获取上一级
            $changeacc = array_diff($pidsarr, $ids);
            foreach ($changeacc as $changeacckey => $changeaccval) {
                $accinfo = Db::name('fin_account')->field('id as account_id,pid,name')->where('id', $changeaccval)->find();
                $accinfo['happen_day'] = $accinfo['adjust'] = $accinfo['id'] = '';
                $accinfo['amount'] = 0;
                if (!in_array($accinfo['account_id'], $accid)) {
                    array_push($accid, $accinfo['account_id']);
                    $accoundatas[] = $accinfo;
                }
            }
        }
        return $accoundatas;
    }
}